package com.jg.poiet.util;

import java.util.HashSet;
import java.util.Set;

public class PatternUtil {

    private final static Set<Character> PATTERN_SPECIAL_CHARACTERS = new HashSet<>();

    static {
        PATTERN_SPECIAL_CHARACTERS.add('$');
        PATTERN_SPECIAL_CHARACTERS.add('(');
        PATTERN_SPECIAL_CHARACTERS.add(')');
        PATTERN_SPECIAL_CHARACTERS.add('*');
        PATTERN_SPECIAL_CHARACTERS.add('+');
        PATTERN_SPECIAL_CHARACTERS.add('.');
        PATTERN_SPECIAL_CHARACTERS.add('[');
        PATTERN_SPECIAL_CHARACTERS.add(']');
        PATTERN_SPECIAL_CHARACTERS.add('?');
        PATTERN_SPECIAL_CHARACTERS.add('\\');
        PATTERN_SPECIAL_CHARACTERS.add('^');
        PATTERN_SPECIAL_CHARACTERS.add('{');
        PATTERN_SPECIAL_CHARACTERS.add('}');
        PATTERN_SPECIAL_CHARACTERS.add('|');
    }

    public static String toPatternStr(String str) {
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (PATTERN_SPECIAL_CHARACTERS.contains(c)) {
                res.append("\\");
            }
            res.append(c);
        }
        return res.toString();
    }

}
